/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package war2100.game.items.ammo;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Vector2f;
import war2100.Prototypes;
import war2100.entity.Map;
import war2100.game.helper.Vector2Helper;
import war2100.game.states.GameplayState;

public class Ammo 
{
	private Image img;
	private Vector2f dir;
	private Vector2f pos;
	private float speed;
	private float dmg;
	private float angle;
	
	private GameplayState gameState;
	private Map map;
	private Prototypes prototypes;
	
	
	public Ammo(GameplayState gs, Prototypes prototypes, Vector2f position, Vector2f dir)
	{
		
		this.dir = dir;
		this.speed = 1f;
		this.gameState = gs;
		this.map = gs.getMap();
		this.prototypes = prototypes;
		
		this.pos = position;
	}
	
	public void update(GameContainer gc, int delta)
	{
		setAngle(Vector2Helper.getTargetAngle(getPosition(), new Vector2f(getPosition().getX() + getDirection().getX(), getPosition().getY() + getDirection().getY())));
		
		
		float faktor = speed*((float)delta)*0.1f;
		
		Vector2f newpos = getPosition().add(new Vector2f(getDirection().getX() * faktor, getDirection().getY() * faktor));
		Vector2f fieldSize = Vector2Helper.getFieldSize();
		Vector2f offset = Vector2Helper.getOffset(map, gc);
		
		float x = (getPosition().getX() - offset.getX()) / fieldSize.getX();
		float y = (getPosition().getY() - offset.getY()) / fieldSize.getY();
		float nx = (newpos.getX() - offset.getX()) /fieldSize.getX();
		float ny = (newpos.getY() - offset.getY()) /fieldSize.getY();
		
		if ((map.getField((int) nx, (int) ny).isWalkable() 
					 && map.getField((int) nx + 1, (int) ny + 1).isWalkable())) {
			setPosition(newpos);
		}else
		{
			this.destroy();
		}
	}
	
	
	
	public void render(Graphics g, GameContainer gc)
	{
		this.getImage().rotate(getAngle());
		this.getImage().draw(getPosition().getX(), getPosition().getY());
		this.getImage().rotate(-getAngle());
	}

	
	public void destroy()
	{
		getGameState().getShoots().remove(this);
	}
	
	
	
	public Ammo copy()
	{
		return new Ammo(getGameState(), prototypes, getPosition(), getDirection());
	}
	
	
	
	/**
	 * @return the img
	 */
	public Image getImage() {
		return img;
	}

	/**
	 * @param img the img to set
	 */
	public void setImage(Image img) {
		this.img = img;
	}

	/**
	 * @return the dir
	 */
	public Vector2f getDirection() {
		return dir;
	}

	/**
	 * @param dir the dir to set
	 */
	public void setDirection(Vector2f dir) {
		this.dir = dir;
	}

	/**
	 * @return the pos
	 */
	public Vector2f getPosition() {
		return pos;
	}

	/**
	 * @param pos the pos to set
	 */
	public void setPosition(Vector2f pos) {
		this.pos = pos;
	}

	/**
	 * @return the speed
	 */
	public float getSpeed() {
		return speed;
	}

	/**
	 * @param speed the speed to set
	 */
	public void setSpeed(float speed) {
		this.speed = speed;
	}

	/**
	 * @return the dmg
	 */
	public float getDamage() {
		return dmg;
	}

	/**
	 * @param dmg the dmg to set
	 */
	public void setDamage(float dmg) {
		this.dmg = dmg;
	}

	/**
	 * @return the prototypes
	 */
	public Prototypes getPrototypes() {
		return prototypes;
	}

	/**
	 * @param prototypes the prototypes to set
	 */
	public void setPrototypes(Prototypes prototypes) {
		this.prototypes = prototypes;
	}

	/**
	 * @return the angle
	 */
	public float getAngle() {
		return angle;
	}

	/**
	 * @param angle the angle to set
	 */
	public void setAngle(float angle) {
		this.angle = angle;
	}

	/**
	 * @return the gameState
	 */
	public GameplayState getGameState() {
		return gameState;
	}

	/**
	 * @param gameState the gameState to set
	 */
	public void setGameState(GameplayState gameState) {
		this.gameState = gameState;
	}
	
}
